package reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class ReflectDemo3 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        Person p = new Person("李四",22);
        System.out.println(p);

        Class cls = Class.forName("reflect.Person");
        /*
        获取Person的构造器Person（String,int)
        cls.getConstructor()//不传任何参数时获取的依然是无参构造器。
         */
        Constructor c =cls.getConstructor(String.class,int.class);
        //new Person("王五",55)
        Object obj = c.newInstance("王五",55);
        System.out.println(obj);
    }
}
